
import React,{useRef,useEffect,useCallback} from'react';

const useState = (initialHooksState)=>{
    const stateRef = useRef();
    const isFirst = useRef(true);
    const [state,_setState] = React.useState(initialHooksState);
    //只创建一次 不用频繁创建
    const setState = useCallback((newState,callback)=>{
        _setState(newState)
        stateRef.current = callback;
    },[]);
    useEffect(()=>{
        if( isFirst.current ){
            isFirst.current = false;
            return;
        }
        stateRef.current(state);
    },[state]);
    return [state,setState];
}
export default useState;